package br.com.werp.suporte.lov;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 03/06/2005 16:02:56
 * Last Modify Date 25/11/2005 11:31:25
 */

public class NumeroAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_select_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      select_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void select_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    NumeroActionForm f = (NumeroActionForm) form;
    br.com.werp.suporte.dbobj.table.Cep table =
      new br.com.werp.suporte.dbobj.table.Cep();
    String[][] select = null;
    String[] order = null;
    String[] groupby = null;
    String[] having = null;
    Object[][] where = { {"Ceps_id","=",
        com.egen.util.text.FormatNumber.toInt(f.getCeps_id())}, {"Ceps_logradouro",
            "=", com.egen.util.text.FormatNumber.toInt(f.getCeps_logradouro())
                                                                }, {"Ceps_bairro","=",
                                                                    com.egen.util.text.FormatNumber.toInt(f.getCeps_bairro())}, {"Ceps_cidade",
                                                                        "=", com.egen.util.text.FormatNumber.toInt(f.getCeps_cidade())}, {"Ceps_numero", "=",
                                                                            com.egen.util.text.FormatNumber.toInt(f.getCeps_numero())} };
    Vector res_Cep = j.select(table, select, where, groupby, having, order);
    session.setAttribute("res_Cep",res_Cep);
    if (res_Cep != null && res_Cep.size() > 0) {
      br.com.werp.suporte.dbobj.table.Cep r =
        (br.com.werp.suporte.dbobj.table.Cep)
        res_Cep.elementAt(0);
      f.setCeps_id(r.getCeps_id() + "");
      f.setCeps_logradouro(r.getCeps_logradouro() + "");
      f.setCeps_bairro(r.getCeps_bairro() + "");
      f.setCeps_cidade(r.getCeps_cidade() + "");
      f.setCeps_numero(r.getCeps_numero() + "");
    } else {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsselected",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_resetfull_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      session.removeAttribute("res_Cep");
      form.reset(mapping, request);
      session.setAttribute(mapping.getName(), form);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

}
